﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace AutoTestEntry.Data
{
    class GlobalInfo
    {
        public static readonly String Version = "alpha_1.3";
        public static readonly String LocalInfoPath = "C:\\Program Files\\AutoTestEntry\\Info.txt";
		public static readonly String UpdateExePath = "C:\\Program Files\\AutoTestEntry\\Update.exe";
        public static readonly String LocalInfoDir = "C:\\Program Files\\AutoTestEntry";

		public static readonly String SaveNetDir = "C:\\";
		public static readonly String SaveSvnDir = "C:\\";
		public static readonly String SaveNunitDir = "C:\\Program Files\\";

		public static readonly String FtpNetPath = "Tools/dotnetfx35.exe";
        public static readonly String FtpSvnPath = "AutoTestEntry/svn-win32-1.6.6/";
        public static readonly String FtpNUnitPath = "AutoTestEntry/NUnit 2.5.7/";
        public static readonly String FtpVersionPath = "AutoTestEntry/Version/";
		public static readonly String FtpNewVersionSoftPath = "AutoTestEntry/AutoTestEntry.exe";

        private static readonly String defaultFtpAddress = "ftp://editor:123456@172.17.225.120";
        private static readonly String defaultSvnServerAddress = "https://svn.baidu.com/app-test/ecom/editor/trunk/ui-test-automation";
        private static readonly String defaultLocalSvnPath = "C:\\svn-win32-1.6.6\\bin\\svn.exe";
        private static readonly String defaultLocalDestPath = "C:\\EAT";
        private static readonly String defaultLocalNetPath = "C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\MSBuild.exe";
        private static readonly String defaultLocalNUnitPath = "C:\\Program Files\\NUnit 2.5.7\\bin\\net-2.0\\nunit-x86.exe";

		private static readonly String defaultSvnUserName = "1";
		private static readonly String defaultSvnPassword = "1";
        
        public static String FtpAddress { get; set; }
        public static String SvnServerAddress { get; set; }
        public static String LocalSvnPath { get; set; }
        public static String LocalDestPath { get; set; }
        public static String LocalNetPath { get; set; }
        public static String LocalNUnitPath { get; set; }

		public static String SvnUserName {get; set; }
		public static String SvnPassword { get; set;}

		public static String NewVersion { get;set;}
        
        
        //禁止实例化
        private GlobalInfo()
        {
        }

        public static bool ReadLocalInfo()
        {
            if (!File.Exists(LocalInfoPath) || !File.Exists(UpdateExePath))
            {
                InitForFirstTimeUsing();
            }
            else
            {
                StreamReader sr = new StreamReader(LocalInfoPath);
                FtpAddress = sr.ReadLine();
                SvnServerAddress = sr.ReadLine();
                LocalSvnPath = sr.ReadLine();
                LocalDestPath = sr.ReadLine();
                LocalNetPath = sr.ReadLine();
                LocalNUnitPath = sr.ReadLine();

				SvnUserName = sr.ReadLine();
				SvnPassword = sr.ReadLine();

                sr.Close();
            }
            return true;
        }

        private static void InitForFirstTimeUsing()
        {
            FtpAddress = defaultFtpAddress;
            SvnServerAddress = defaultSvnServerAddress;
            LocalSvnPath = defaultLocalSvnPath;
            LocalDestPath = defaultLocalDestPath;
            LocalNetPath = defaultLocalNetPath;
            LocalNUnitPath = defaultLocalNUnitPath;

			SvnUserName = defaultSvnUserName;
			SvnPassword = defaultSvnPassword;

		
            SaveLocalInfo();
			SaveUpdateExe();
        }

        public static void SaveLocalInfo()
        {
            StreamWriter sw;
            if (!File.Exists(LocalInfoPath))
            {
                if (!Directory.Exists(LocalInfoDir))
                {
                    Directory.CreateDirectory(LocalInfoDir);
                }
                sw = File.CreateText(LocalInfoPath);
            }
            else
            {
                sw = new StreamWriter(LocalInfoPath);
            }
            sw.WriteLine(FtpAddress);
            sw.WriteLine(SvnServerAddress);
            sw.WriteLine(LocalSvnPath);
            sw.WriteLine(LocalDestPath);
            sw.WriteLine(LocalNetPath);
            sw.WriteLine(LocalNUnitPath);

			sw.WriteLine(SvnUserName);
			sw.WriteLine(SvnPassword);
            sw.Flush();
            sw.Close();
        }

		private static void SaveUpdateExe()
		{
			String path = GlobalInfo.LocalInfoDir + "\\update.exe";

			FileStream fs = new FileStream(path, FileMode.Create);
			BinaryWriter bw = new BinaryWriter(fs);

			bw.Write(Resource.Update);
			bw.Flush();
			bw.Close();
		}

    }
}
